package com.googlecode.perfectable.prosperable.webchat.message;

import static com.google.common.base.Preconditions.checkState;

import org.codehaus.jackson.annotate.JsonProperty;

import com.googlecode.perfectable.prosperable.message.ServerMessage;
import com.googlecode.perfectable.webchat.Message;

public class LogPublishedMessage implements ServerMessage {
	private static final long serialVersionUID = -5526401483056545842L;
	
	public static LogPublishedMessage create(Message event) {
		class LogPublishedMessageCreatingWriter implements Message.Writer {
			private String writtenBody;

			@Override
			public void writeBody(String body) {
				this.writtenBody = body;
			}

			LogPublishedMessage finish() {
				checkState(this.writtenBody != null, "No message body");
				return LogPublishedMessage.of(this.writtenBody);
			}
		}
		LogPublishedMessageCreatingWriter writer = new LogPublishedMessageCreatingWriter();
		event.write(writer);
		return writer.finish();
	}
	
	public static LogPublishedMessage of(String body) {
		return new LogPublishedMessage(body);
	}

	private final String body;

	private LogPublishedMessage(String body) {
		this.body = body;
	}
	
	@JsonProperty("body")
	private final String getBody() {
		return this.body;
	}
	
}
